package com.bjpowernode.common.pay;

import cn.hutool.json.JSONUtil;
import com.bjpowernode.common.client.KuaiQianHttpUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * 支付工具类
 */
public class PayUtil {

    public static String appendParam(String returns, String paramId, String paramValue) {
        if (returns != "") {
            if (paramValue != "" && paramValue != null) {
                returns += "&" + paramId + "=" + paramValue;
            }
        } else {
            if (paramValue != "" && paramValue != null) {
                returns = paramId + "=" + paramValue;
            }
        }
        return returns;
    }

    /**
     * 对订单进行订单结果的查询
     *
     * @param oid
     * @return
     */
    public static String queryOrderId(String oid) {
        Map<String, Object> request = new HashMap<String, Object>();
        //固定值：1代表UTF-8;
        String inputCharset = "1";
        //固定值：v2.0 必填
        String version = "v2.0";
        //1代表Md5，2 代表PKI加密方式  必填
        String signType = "2";
        //人民币账号 membcode+01  必填
        String merchantAcctId = "1001214035601";
        //固定值：0 按商户订单号单笔查询，1 按交易结束时间批量查询必填
        String queryType = "0";
        //固定值：1	代表简单查询 必填
        String queryMode = "1";
        //数字串，格式为：年[4 位]月[2 位]日[2 位]时[2 位]分[2 位]秒[2位]，例如：20071117020101
        String startTime = "";
        ////数字串，格式为：年[4 位]月[2 位]日[2 位]时[2 位]分[2 位]秒[2位]，例如：20071117020101
        String endTime = "";
        String requestPage = "";
        String orderId = oid;
        //查询秘钥
        String key = "27YKWKBKHT2IZSQ4";

        request.put("inputCharset", inputCharset);
        request.put("version", version);
        request.put("signType", signType);
        request.put("merchantAcctId", merchantAcctId);
        request.put("queryType", queryType);
        request.put("queryMode", queryMode);
        request.put("startTime", startTime);
        request.put("endTime", endTime);
        request.put("requestPage", requestPage);
        request.put("orderId", orderId);

        String message = "";
        message = appendParam(message, "inputCharset", inputCharset);
        message = appendParam(message, "version", version);
        message = appendParam(message, "signType", signType);
        message = appendParam(message, "merchantAcctId", merchantAcctId);
        message = appendParam(message, "queryType", queryType);
        message = appendParam(message, "queryMode", queryMode);
        message = appendParam(message, "startTime", startTime);
        message = appendParam(message, "endTime", endTime);
        message = appendParam(message, "requestPage", requestPage);
        message = appendParam(message, "orderId", orderId);
        message = appendParam(message, "key", key);
        Pkipair pki = new Pkipair();
        String sign = pki.signMsg(message);

        request.put("signMsg", sign);

        //sandbox提交地址
        String reqUrl = "https://sandbox.99bill.com/gatewayapi/gatewayOrderQuery.do";
        String response = "";
        try {
            //connectTimeout网络超时连接时间    socketTimeOut网络通讯数据超时时间
            response = KuaiQianHttpUtil.doPostJsonRequestByHttps(JSONUtil.toJsonStr(request), reqUrl, 10000, 8000);
        } catch (Exception e) {
            response = "error";
            e.printStackTrace();
        }
        System.out.println("response = " + response);
        return response;
    }

}
